﻿using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Caching;
using OZDoo.Core.Caching;
using OZDoo.Core.Exceptions;

namespace OZDoo.Core
{
    
    /// <summary>
    /// 读取ozd程序集默认或指定的配置
    /// </summary>   
    public class OzdConfiguration
    {
        /// <summary>
        /// 缓存默认的根KEY标识
        /// </summary>
        public static readonly string CacheKey = "OzdConfiguration";
        /// <summary>
        /// 为系统提供基本服务的提供者配置标识
        /// </summary>
        public static readonly string ProviderSectionName = "OzdProviders";
        private static OzdConfiguration ozdConfig=null;
        string filesPath="/";
        string defaultLanguage = "default";

        /// <summary>
        /// 默认语言
        /// </summary>
        public string DefaultLanguage
        {
            get { return defaultLanguage; }
            set { defaultLanguage = value; }
        }
        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilesPath
        {
            get { return filesPath; }
        }
        

		#region 配置
        Configuration config = null;
        Hashtable providers = new Hashtable();
		#endregion

		#region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="path">配置文件路径</param>
        public OzdConfiguration(string path)
		{
            LoadConfiguration(path);
            Initialize();
		}
		#endregion

        #region GetSection
        /// <summary>
        /// 得到指定名称的配置节
        /// </summary>
        /// <param name="name">要获取的配置名称</param>
        /// <returns>配置节</returns>
        public ConfigurationSection GetSection(string name)
		{
            return config.GetSection(name);
		}

		#endregion

	    #region GetConfig
        /// <summary>
        /// 获得ozd配置的实例
        /// </summary>
        /// <returns>配置类实例</returns>
        public static OzdConfiguration GetConfig() 
		{
            ozdConfig=CacheManage.GetCache().Get(CacheKey) as OzdConfiguration;
            if(ozdConfig == null)Refresh();
            if (ozdConfig == null) throw new OzdException("配置文件实例化失败");
            return ozdConfig;
        }

        /// <summary>
        /// 刷新配置文件
        /// </summary>
        public static void Refresh()
        {
            if (ozdConfig == null)
            {
                string path = null;
                path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Ozd.config");
                ozdConfig = new OzdConfiguration(path);
                CacheDependency dp = new CacheDependency(path);
                CacheManage.GetCache().Insert(CacheKey,ozdConfig,dp);
            }
            if (ozdConfig == null) throw new OzdException("config_init_error");
        }

        #endregion

        #region LoadConfiguration and Initialize
        /// <summary>
        /// 加载指定配置文件
        /// </summary>
        /// <param name="path">配置文件路径</param>
		internal void LoadConfiguration(string path)
        {
            if (!System.IO.File.Exists(path))
            {
                throw new OzdException("config_file_no_exist");
            }
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = @path;
            config= ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        }
        /// <summary>
        /// 初始化操作
        /// </summary>
        private void Initialize()
        {
        
        }
        #endregion

        /// <summary>
        /// 获得提供程序
        /// </summary>
        public Hashtable Providers {
            get { return providers; }
        }   
    }
}